#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import datetime
import re
import django.utils.simplejson
import string
import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.ext import db
from appengine.billmodel import BillCategory
from appengine.billmodel import Bill

class LoadBillData(webapp.RequestHandler):
	def get(self):
		categories = db.GqlQuery("SELECT * FROM BillCategory WHERE AccountName=:1", self.request.get("user"))
		category_list = [];
		for category in categories:
			category_list.append([category.Name, category.Name])
		self.response.out.write("var category_data = "+django.utils.simplejson.dumps(category_list)+";")

		search_date = datetime.date.today() - datetime.timedelta(6 * 30)
		today = datetime.date.today()
		bills = db.GqlQuery("SELECT * FROM Bill WHERE AccountName=:1 AND CreateTime>=:2 ORDER BY CreateTime ASC", self.request.get("user"), datetime.date(search_date.year, search_date.month, 1))

		curbill_list = []
		bill_category = {}
		bill_month = {}
		for bill in bills:
			if today.month == bill.CreateTime.month:
				if bill_category.has_key(bill.BillCategory):
					bill_category[bill.BillCategory] += bill.PayAmount
				else:
					bill_category[bill.BillCategory] = bill.PayAmount
				curbill_list.append([bill.key().id(), bill.PayAmount, bill.BillCategory, bill.Remark, bill.CreateTime.strftime("%Y-%m-%d")])
			bill_month_key = "%(year)04d-%(month)02d" % {"year":bill.CreateTime.year, "month":bill.CreateTime.month}
			if bill_month.has_key(bill_month_key):
				bill_month[bill_month_key] += bill.PayAmount
			else:
				bill_month[bill_month_key] = bill.PayAmount
		self.response.out.write("var bill_data = "+django.utils.simplejson.dumps(curbill_list)+";")

		chart1_data = []
		bill_month_keys = bill_month.keys()
		bill_month_keys.sort()
		for key in bill_month_keys:
			chart1_data.append([key, ("%0.02f" % bill_month[key])])
		self.response.out.write("var chart1_data = "+django.utils.simplejson.dumps(chart1_data)+";")

		chart2_data = []
		bill_category_keys = bill_category.keys()
		bill_category_keys.sort()
		for key in bill_category_keys:
			chart2_data.append([key, ("%0.02f" % bill_category[key])])
		self.response.out.write("var chart2_data = "+django.utils.simplejson.dumps(chart2_data)+";")

application = webapp.WSGIApplication([('/interface/load.php', LoadBillData)], debug=True)

def main():
	wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
	main()

